package com.sinoglobal.wallet.activity;

import com.sinoglobal.wallet.R;

import android.annotation.SuppressLint;
import android.content.Intent;

import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.entity.W_DynamicKeyEntity;
import com.sinoglobal.wallet.entity.W_SinoBaseEntity;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.NetWorkUtil;
import com.sinoglobal.wallet.widget.WGridPasswordView;
import com.sinoglobal.wallet.widget.WGridPasswordView.PasswordListener;
import com.sinoglobal.wallet.widget.KeyboardUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * 
 * @author lijingjing
 * @createdate 2015-10-23 上午9:58:29
 * @Description: TODO(确定支付密码)
 */
public class W_PayPasswordSureOK extends W_SinoBaseActivity{

	private static final String TAG = W_PayPasswordSureOK.class.getSimpleName();
	private WGridPasswordView gpvNormal1, gpvNormal2;
	private TextView payPassSure_ok, payPassSure_tv1, payPassSure_tv2;
	private String flag,tit_flag;
	private KeyboardUtil keyUtil1;
	private KeyboardUtil keyUtil2;
	private W_PayPasswordSureOK context, act;
	private String key_id, key_key;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		act = context = this;
		setContentView(R.layout.wallet_pay_pass_sure_activity);
		init();
	}

	@SuppressLint("ClickableViewAccessibility") 
	private void init(){
		flag = getIntent().getStringExtra("flag");
		tit_flag = getIntent().getStringExtra("tit");
		if(tit_flag.toString().equals("3")){
			mTemplateTitleText.setText("支付密码");
		}else if(tit_flag.toString().equals("4")){
			mTemplateTitleText.setText("找回支付密码");
		}else{
			mTemplateTitleText.setText("支付密码");
		}
		gpvNormal1 = (WGridPasswordView) findViewById(R.id.gpv_normal1);
		gpvNormal1.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.d(TAG, "gpvNormal1 onTouch");
				keyUtil1 = new KeyboardUtil(act, context, gpvNormal1.getEditText());
				gpvNormal1.setKeyUtil(keyUtil1);
				keyUtil1.showKeyboard();
				return false;
			}
		});	
		gpvNormal2 = (WGridPasswordView) findViewById(R.id.gpv_normal2);
		gpvNormal2.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.d(TAG, "gpvNormal2 onTouch");
				keyUtil2 = new KeyboardUtil(act, context, gpvNormal2.getEditText());
				gpvNormal2.setKeyUtil(keyUtil2);
				keyUtil2.showKeyboard();
				return false;
			}
		});	
		gpvNormal2.setPasswordListener(new PasswordListener() {
			
			@Override
			public void setPassWord(String pwd) {
				if(pwd.length()==6){
					if(keyUtil2.getShowKeyboard()){
						keyUtil2.hideKeyboard();
					}
				}
			}
		});
		gpvNormal1.getEditText().requestFocus();
		payPassSure_ok = (TextView) findViewById(R.id.payPassSure_ok);
		payPassSure_ok.setOnClickListener(this);
		payPassSure_tv1 = (TextView) findViewById(R.id.payPassSure_tv1);
		payPassSure_tv2 = (TextView) findViewById(R.id.payPassSure_tv2);
		if(flag.equals("1")){
			payPassSure_tv1.setText("输入新支付密码");
			payPassSure_tv2.setText("确认新支付密码");
		}else if(flag.equals("2")){
			payPassSure_tv1.setText("请设置6位数字支付密码");
			payPassSure_tv2.setText("再次输入支付密码");
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.payPassSure_ok){
			if(gpvNormal1.getPassWord().toString().equals("")){
				showToast("请填写密码");
			}else if(gpvNormal2.getPassWord().toString().equals("")){
				showToast("请填写确认密码");
			}else if(!gpvNormal1.getPassWord().toString().equals(gpvNormal2.getPassWord().toString())){
				showToast("两次密码输入不一致");
			}else{
				//showToast("one:" + gpvNormal1.getPassWord() + "two:" + gpvNormal2.getPassWord());
				getDynamickey();
			}
		}else if (id == R.id.wallet_noData) {
			 if(NetWorkUtil.getNetWorkInfoType(getApplicationContext())==NetWorkUtil.NONE){
					showToast("请检查网络是否连接！");	
					 loadError(true);
					}else{
						loadError(false);;
					}
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "onRestart");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
	 //获取秘钥
  	private void getDynamickey(){

  		MyAsyncTask<W_DynamicKeyEntity> task = new MyAsyncTask<W_DynamicKeyEntity>(true,this,true) {

  			@Override
  			public W_DynamicKeyEntity before(Void... params) throws Exception {
  				// TODO 测试接口
  				return RemoteImpl.getInstance().getDynamicKey();
  			}
  			@Override
  			public void after(W_DynamicKeyEntity result) {
  				key_id = result.getId();
  				key_key = result.getEncryptKey();
  				settingPassWord();
  			}

  			@Override
  			public void exception() {
  			}
  		};
  		task.execute();
  	
  	
  	}

	private void settingPassWord(){

		MyAsyncTask<W_SinoBaseEntity> task = new MyAsyncTask<W_SinoBaseEntity>(true,this,true) {

			@Override
			public W_SinoBaseEntity before(Void... params) throws Exception {
				// TODO 测试接口
				Log.d("。。。", key_key + "......" + key_id);
				return RemoteImpl.getInstance().settingPassWord(W_PayPasswordSureOK.this, gpvNormal1.getPassWord().toString()
						,key_id, key_key);
			}

			@Override
			public void after(W_SinoBaseEntity result) {

				if (result.getCode().equals("100")) {
					if(result != null){
						showToast("密码设置成功");
						sendBroadcast();//通知主页面隐藏设置密码的布局
						sendWithdrawalBroadcast();//通知提现页面isHaspass值更改为Y
						finish();
					}
				} else {
					showToast(result.getMsg());
				}

			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
	private void sendBroadcast() {
		Intent intent = new Intent();
		intent.setAction(SinoConstans.KEY_BalanceReceiver);
		intent.putExtra("hidePassWord", true);
		sendBroadcast(intent);
	}
	private void sendWithdrawalBroadcast() {
		Intent intent = new Intent();
		intent.setAction("com.sinoglobal.wallet.receiver.withdrawal");
		intent.putExtra("isPass", "Y");
		intent.putExtra("type", 301);
		sendBroadcast(intent);
	}
}
